﻿using AnesSystem.Models;
using AnesSystem.ViewModels.Operation.CQ.CQTL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.Operation.CQ.CQTL
{
    /// <summary>
    /// 麻醉单用药
    /// </summary>
    public partial class AnesMedicineCtrl : UserControl
    {

        #region Variables
        private AnesMedicineCtrlViewModel _model;
        #endregion

        #region Constructor
        public AnesMedicineCtrl()
        {
            InitializeComponent();
            InitHorizontalLins();
            this.Loaded += AnesMedicineCtrl_Loaded;
            InitTimeLins();

        }

        private void AnesMedicineCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            _model = this.DataContext as AnesMedicineCtrlViewModel;
            //InitTimeLins();
        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods
        private void InitTimeLins()
        {
            //int curPage = _model.CurPage;
            DateTime anesRecordTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            //DateTime startTime = anesRecordTime.AddHours((curPage - 1) * 4);
            int startX = LocationUtil.Medicine_Left_Width;
            int startY = 0;
            DateTime date = anesRecordTime;
            for (int i = 0; i <= LocationUtil.TotalCol; i++)
            {
                Line line = new Line();
                line.X1 = startX;
                line.Y1 = startY;
                line.X2 = startX;
                line.Y2 = LocationUtil.Medicine_Height * LocationUtil.Medicine_Rows;
                startX += LocationUtil.RealTimeItem_Width;
                if (i == 0 || i == 48)
                {
                    line.Stroke = Brushes.Black;
                    line.StrokeThickness = 1;
                }
                else
                {
                    if (date.Minute % 30 == 0)
                    {
                        line.Stroke = Brushes.Black;
                        line.StrokeThickness = 1;
                        //if (date.Minute % 60 == 0)
                        //{
                        //    line.Stroke = Brushes.Black;
                        //    line.StrokeThickness = 2;
                        //}
                    }
                    else
                    {
                        line.Stroke = Brushes.Black;
                        line.StrokeDashArray = new DoubleCollection() { 2, 3 };
                        line.StrokeDashCap = PenLineCap.Flat;
                        line.StrokeEndLineCap = PenLineCap.Flat;
                        line.StrokeStartLineCap = PenLineCap.Flat;
                        line.StrokeThickness = 0.5;
                    }
                }
                _canvas.Children.Add(line);
                date = date.AddMinutes(5);
            }
        }

        private void InitHorizontalLins()
        {
            int startX0 = 0;

            int startX = LocationUtil.Medicine_Left_Width;
            int startY = 0;

            for (int i = 0; i < LocationUtil.Medicine_Rows; i++)
            {
                //中间时间格子线
                Line line = new Line();
                line.X1 = startX;
                line.Y1 = startY;

                line.X2 = LocationUtil.Medicine_Left_Width + LocationUtil.TotalCol * LocationUtil.RealTimeItem_Width;
                line.Y2 = startY;

                //左边药名格子线
                Line line0 = new Line();
                line0.X1 = startX0;
                line0.Y1 = startY;
                line0.X2 = LocationUtil.Medicine_Left_Width - 1;
                line0.Y2 = startY;

                startY += LocationUtil.Medicine_Height;

                if (i % 2 != 0)
                {
                    line.Stroke = Brushes.Black;
                    line.StrokeThickness = 1;
                    line.StrokeDashArray = new DoubleCollection() { 2, 3 };
                    line.StrokeDashCap = PenLineCap.Flat;
                    line.StrokeEndLineCap = PenLineCap.Flat;
                    line.StrokeStartLineCap = PenLineCap.Flat;
                }
                else
                {
                    line.Stroke = Brushes.Black;
                    line.StrokeThickness = 0.5;
                }

                line0.Stroke = new SolidColorBrush(Color.FromRgb(100, 100, 100));
                line0.StrokeDashOffset = 1;

                _canvas.Children.Add(line0);
                _canvas.Children.Add(line);
            }
        }

        #endregion

        #region EventHandlers

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                MedicineConfigCtrlViewModel.Instance.DialogType = 1;
                MedicineConfigCtrlViewModel.Instance.PatientDrugsModel = new PatientDrugsModel();
                MedicineConfigCtrlViewModel.Instance.CurPage = (this.DataContext as AnesMedicineCtrlViewModel).CurPage;
                MedicineConfigCtrlViewModel.Instance.SetPatientDrug();
                DrugAndEventWindow.Instance.CusContent = MedicineConfigDialogCtrl.Instance;
                DrugAndEventWindow.Instance.Title = "编辑用药输液";
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void DrugTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                PatientDrugsModel p = (sender as TextBlock).DataContext as PatientDrugsModel;
                MedicineConfigCtrlViewModel.Instance.DialogType = 1;
                MedicineConfigCtrlViewModel.Instance.PatientDrugsModel = p;
                MedicineConfigCtrlViewModel.Instance.CurPage = (this.DataContext as AnesMedicineCtrlViewModel).CurPage;
                MedicineConfigCtrlViewModel.Instance.SetPatientDrug();
                DrugAndEventWindow.Instance.CusContent = MedicineConfigDialogCtrl.Instance;
                DrugAndEventWindow.Instance.Title = "编辑用药输液";
                DrugAndEventWindow.Instance.ShowDialog();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        #endregion
    }
}
